/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package login.bean;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Javier
 */
@ManagedBean
@RequestScoped
public class PrincipalPageBean {
    
    @ManagedProperty(value="#{loginBean.user}")
    public String user;
    @ManagedProperty(value="#{loginBean.currentConnection}")
    public Connection conn;
    public static String centerPage = "vacio";

    public PrincipalPageBean() {
        
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }
   
    public String getUser() {
        return user;
    }

    public String getCenterPage() {
        return centerPage;
    }

    public void setCenterPage(String centerPage) {
        FacesContext fc2 = FacesContext.getCurrentInstance();
        if (fc2.getExternalContext().getSessionMap().containsKey("tableBean")) {
            System.out.println("entro a borrar");
            fc2.getExternalContext().getSessionMap().remove("tableBean");
        }
        FacesContext fc = FacesContext.getCurrentInstance();
        if (fc.getExternalContext().getSessionMap().containsKey("advancedPrivilegesBean")) {
            System.out.println("entro a borrar");
            fc.getExternalContext().getSessionMap().remove("advancedPrivilegesBean");
        }
        this.centerPage = centerPage;
    }
    
    public void setUser(String user) {
        this.user = user;
    }
    public void reset() {
        ExternalContext ctx
                = FacesContext.getCurrentInstance().getExternalContext();
        String ctxPath
                = ((ServletContext) ctx.getContext()).getContextPath();
        try {
            ((HttpSession) ctx.getSession(false)).invalidate();
                ctx.redirect(ctxPath + "/faces/index.xhtml");
        } catch (IOException ex) {
            System.out.println("entra catch");
            ex.printStackTrace();
        }
        try {
            conn.close();
            PrincipalPageBean.centerPage = "vacio";
        } catch (SQLException ex) {
            System.out.println("NO EXISTE CONEXIÓN");
        }
    }
}
